﻿using System;
using System.Xml;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000ACE RID: 2766
	internal class AIRuntimeSetDest : AIRunTimeNodeAction
	{
		// Token: 0x0600A59A RID: 42394 RVA: 0x001CCD60 File Offset: 0x001CAF60
		public AIRuntimeSetDest(XmlElement node) : base(node)
		{
			this._final_dest_name = node.GetAttribute("Shared_FinalDestName");
			this._target_name = node.GetAttribute("Shared_TargetName");
			this._nav_name = node.GetAttribute("Shared_NavName");
			this._born_pos_name = node.GetAttribute("Shared_BornPosName");
			this._tick_count_name = node.GetAttribute("Shared_TickCountName");
			this._random_max = float.Parse(node.GetAttribute("RandomMax"));
			this._adjust_angle = float.Parse(node.GetAttribute("AdjustAngle"));
			this._adjust_length_name = node.GetAttribute("Shared_AdjustLengthName");
			this._adjust_value = float.Parse(node.GetAttribute("Shared_AdjustLengthmValue"));
			this._adjust_dir = int.Parse(node.GetAttribute("AdjustDir"));
			this._set_dest_type = int.Parse(node.GetAttribute("SetDestType"));
			string attribute = node.GetAttribute("Shared_BornPosmValue");
			float num = float.Parse(attribute.Split(new char[]
			{
				':'
			})[0]);
			float num2 = float.Parse(attribute.Split(new char[]
			{
				':'
			})[1]);
			float num3 = float.Parse(attribute.Split(new char[]
			{
				':'
			})[2]);
			this._born_pos = new Vector3(num, num2, num3);
		}

		// Token: 0x0600A59B RID: 42395 RVA: 0x001CCEB0 File Offset: 0x001CB0B0
		public override bool Update(XEntity entity)
		{
			Vector3 vector = entity.AI.AIData.GetVector3ByName(this._final_dest_name, Vector3.zero);
			XGameObject xgameObjectByName = entity.AI.AIData.GetXGameObjectByName(this._target_name);
			Transform transformByName = entity.AI.AIData.GetTransformByName(this._nav_name);
			Vector3 vector2 = entity.AI.AIData.GetVector3ByName(this._born_pos_name, Vector3.zero);
			int intByName = entity.AI.AIData.GetIntByName(this._tick_count_name, 0);
			float floatByName = entity.AI.AIData.GetFloatByName(this._adjust_length_name, this._adjust_value);
			bool flag = string.IsNullOrEmpty(this._born_pos_name);
			if (flag)
			{
				vector2 = this._born_pos;
			}
			Vector3 vector3 = new Vector3(1f, 0f, 1f);
			int num = XFastEnumIntEqualityComparer<SetDestWay>.ToInt(SetDestWay.Target);
			bool flag2 = this._set_dest_type == XFastEnumIntEqualityComparer<SetDestWay>.ToInt(SetDestWay.Target);
			if (flag2)
			{
				bool flag3 = xgameObjectByName == null;
				if (flag3)
				{
					return false;
				}
				vector = xgameObjectByName.Position;
			}
			else
			{
				bool flag4 = this._set_dest_type == XFastEnumIntEqualityComparer<SetDestWay>.ToInt(SetDestWay.BornPos);
				if (flag4)
				{
					vector = vector2;
				}
				else
				{
					bool flag5 = this._set_dest_type == XFastEnumIntEqualityComparer<SetDestWay>.ToInt(SetDestWay.NavPos);
					if (flag5)
					{
						bool flag6 = transformByName == null;
						if (flag6)
						{
							return false;
						}
						vector = transformByName.position;
					}
				}
			}
			bool flag7 = floatByName != 0f;
			if (flag7)
			{
				Vector3 vector4 = Vector3.zero;
				bool flag8 = this._adjust_dir == XFastEnumIntEqualityComparer<AdjustDirection>.ToInt(AdjustDirection.TargetDir);
				if (flag8)
				{
					vector4 = entity.EngineObject.Position - vector;
				}
				else
				{
					bool flag9 = this._adjust_dir == XFastEnumIntEqualityComparer<AdjustDirection>.ToInt(AdjustDirection.TargetFace) && xgameObjectByName != null;
					if (flag9)
					{
						vector4 = xgameObjectByName.Forward.normalized;
					}
					else
					{
						bool flag10 = this._adjust_dir == XFastEnumIntEqualityComparer<AdjustDirection>.ToInt(AdjustDirection.SelfFace);
						if (flag10)
						{
							vector4 = entity.EngineObject.Forward.normalized;
						}
					}
				}
				Vector3 vector5 = vector + (Quaternion.Euler(new Vector3(0f, (float)(intByName % 2) * this._adjust_angle * 2f - this._adjust_angle, 0f)) * vector4).normalized * floatByName;
				bool flag11 = !XSingleton<XAIGeneralMgr>.singleton.IsPointInMap(vector5);
				if (flag11)
				{
					for (int i = 0; i < 18; i++)
					{
						float num2 = this._adjust_angle + (float)(i * 10);
						vector5 = vector + (Quaternion.Euler(new Vector3(0f, (float)(intByName % 2) * num2 * 2f - num2, 0f)) * vector4).normalized * floatByName;
						bool flag12 = XSingleton<XAIGeneralMgr>.singleton.IsPointInMap(vector5);
						if (flag12)
						{
							break;
						}
						num2 = this._adjust_angle - (float)(i * 10);
						vector5 = vector + (Quaternion.Euler(new Vector3(0f, (float)(intByName % 2) * num2 * 2f - num2, 0f)) * vector4).normalized * floatByName;
						bool flag13 = XSingleton<XAIGeneralMgr>.singleton.IsPointInMap(vector5);
						if (flag13)
						{
							break;
						}
					}
				}
				vector = vector5;
			}
			bool flag14 = this._random_max > 0f;
			if (flag14)
			{
				vector3.x = XSingleton<XCommon>.singleton.RandomFloat(-0.5f, 0.5f);
				vector3.z = XSingleton<XCommon>.singleton.RandomFloat(-0.5f, 0.5f);
				vector += this._random_max * vector3.normalized;
			}
			entity.AI.AIData.SetVector3ByName(this._final_dest_name, vector);
			return true;
		}

		// Token: 0x04003C8F RID: 15503
		private string _final_dest_name;

		// Token: 0x04003C90 RID: 15504
		private string _target_name;

		// Token: 0x04003C91 RID: 15505
		private string _nav_name;

		// Token: 0x04003C92 RID: 15506
		private string _born_pos_name;

		// Token: 0x04003C93 RID: 15507
		private Vector3 _born_pos;

		// Token: 0x04003C94 RID: 15508
		private string _tick_count_name;

		// Token: 0x04003C95 RID: 15509
		private float _random_max;

		// Token: 0x04003C96 RID: 15510
		private float _adjust_angle;

		// Token: 0x04003C97 RID: 15511
		private string _adjust_length_name;

		// Token: 0x04003C98 RID: 15512
		private float _adjust_value;

		// Token: 0x04003C99 RID: 15513
		private int _adjust_dir;

		// Token: 0x04003C9A RID: 15514
		private int _set_dest_type;
	}
}
